<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

/*
 * 前台资源路由配置
 */
define('INDEX_CSS', "/static/index/css/");
define('INDEX_JS', "/static/index/js/");
define('INDEX_IMG', "/static/index/images/");
define('INDEX_VENDOR', "/static/index/vendor/");
define('INDEX_LIMIT', 15);
/*
 * 前台资源路由配置
 */
define('ADMIN_CSS', "/static/admin/css/");
define('ADMIN_JS', "/static/admin/js/");
define('ADMIN_IMG', "/static/admin/images/");
define('ADMIN_FONTS', "/static/admin/fonts/");
define('ADMIN_VENDOR', "/static/admin/vendor/");
define('ADMIN_LIMIT', 15);

use Lib\Page;
use Lib\Vcode;
// 应用公共文件
function model()
{
    //表模型
    $model['news'] = db('news');
    return $model;
}


function dd($id)
{
    $data = model()['news']->where(['id' => $id])->value('title');
    return $data;
}


function upload_img($name = "", $file_name = "")
{
    $file = request()->file($name);
    $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads/' . $file_name);
    if ($info) {
        $return['info'] = DS . 'uploads/' . $file_name . "/" . $info->getSaveName();
        $return['status'] = 200;
    } else {
        $return['info'] = $file->getError();
        $return['status'] = 404;
    }
    return $return;
}

/*
 * 验证手机号码
 */

function checkphone($phone)
{
    $preg_phone = '/^1[34578]\d{9}$/ims';
    if (preg_match($preg_phone, $phone)) {
        return 1;
    } else {
        return 0;
    }
}

/*
 *验证身份证号码
 */

function checkcard($id_card)
{
    if (mb_strlen($id_card) != 18) return false;
    //校验位列表
    $remainder_list = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
    //加权除以11的余数
    $square_remainder = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
    //取得身份证号码最后一位校验位
    $check_num = mb_substr($id_card, 17);
    //身份证参与校验的前17位
    $id_card = mb_substr($id_card, 0, 17);
    //参与校验的必须是数字
    if (!is_numeric($id_card)) return false;
    $square_sum = 0;//每一位参与校验数乘以加权数的累加和
    $number_sum = 0;//每一位参与校验数乘以加权除以11的余数的累加和
    for ($i = 0; $i < 17; $i++) {
        //累加结果
        $square_sum += intval($id_card[$i]) * pow(2, 17 - $i);
        //累加结果
        $number_sum += intval($id_card[$i]) * $square_remainder[$i];
    }
    //从校验位列表中获取加权乘积和得到的校验位
    $check_get_square_remainder = isset($remainder_list[$square_sum % 11]) ? $remainder_list[$square_sum % 11] : -1;
    //从校验位列表中获取加权余数乘积和得到的校验位
    $check_get_number_remainder = isset($remainder_list[$number_sum % 11]) ? $remainder_list[$number_sum % 11] : -1;
    if ($check_get_square_remainder == $check_num && $check_get_number_remainder == $check_num) return true;
    return false;
}


/*
 *
 */
function space($goodid)
{
    $goodid = preg_replace('# #', '', $goodid);
    return $goodid;
}

///*
// *  获取数据
// */
//function get_data($data, $name = "", $time = 10)
//{
//    if (cache($name)) {
//        return "缓存";
//    } else {
//        cache($name, $data, $time);
//        return "202";
//    }
//}

function page($count, $limit, $name = "")
{
    $page = new  Page($count, $limit);
    return $page->showPages($name);
}


function get_data()
{
    $arr = array();
    for ($i = 1; $i < 25; $i++) {
        if($i<10){
            $str="0".$i.":"."00";
        }else{
            $str=$i.":"."00";
        }
        $arr[]=$str;
     }
     return $arr;
}

function Vcode(){
    $code=new Vcode();
    $aa=$code->outimg();
    session('code',$code->getcode());
    return  $aa;
}